package com.bjpowernode.controller;
import com.bjpowernode.beans.Stu;
import com.bjpowernode.dto.ResultDTO;
import com.bjpowernode.exception.MyDateExceptionAdvice;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;

@RestController
@RequestMapping("/")
public class IndexController {

    @RequestMapping("get.action")
    public ResultDTO get(Stu stu, BindingResult bindingResult) throws MyDateExceptionAdvice {
        if(bindingResult.hasErrors()){
            throw new MyDateExceptionAdvice("生日格式输入错误");
        }
        ResultDTO resultDTO=new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("name",stu.getName());
        LocalDate age = stu.getAge();
        String strAge = age.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        hashMap.put("age",strAge);
        resultDTO.setData(hashMap);
        return resultDTO;
    }
}
